﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace Rescue_of_the_Dwarvenzombies.Rendering.RenderStates
{
    /// <summary>
    /// The RenderStateManager keeps track of the different draw methods to be used during the different GameStates.
    /// </summary>
    /// <remarks></remarks>
    public class RenderStateManager
    {
        /// <summary>
        /// A stack of renderstates in the right order. A new RenderState is pushed to the stack when 
        /// entering a new state, such as PauseState. 
        /// </summary>
        private Stack<RenderState> _renderStates = new Stack<RenderState>();

        protected Game Game;

        /// <summary>
        /// Initializes a new instance of the <see cref="RenderStateManager"/> class.
        /// </summary>
        /// <param name="game">The game.</param>
        /// <remarks></remarks>
        public RenderStateManager(Game game)
        {
            Game = game;
            //For now, set initial state to Playing state
            ChangeState(new PlayingRenderState(game, this));
        }

        /// <summary>
        /// Changes the state.
        /// </summary>
        /// <param name="newState">The new state.</param>
        /// <remarks></remarks>
        public void ChangeState(RenderState newState)
        {
            if (!ContainsState(newState))
            {
                _renderStates.Push(newState);
                ((IRenderManager)Game.Services.GetService(typeof(IRenderManager))).CurrentState = newState;
            }
            else
            {
                _renderStates.Pop();
                ((IRenderManager)Game.Services.GetService(typeof(IRenderManager))).CurrentState = CurrentState;
            }
        }

        /// <summary>
        /// Determines whether the specified new state contains state.
        /// </summary>
        /// <param name="newState">The new state.</param>
        /// <returns><c>true</c> if the specified new state contains state; otherwise, <c>false</c>.</returns>
        /// <remarks></remarks>
        public bool ContainsState(RenderState newState)
        {
            return _renderStates.Contains(newState);
        }

        /// <summary>
        /// Gets the state of the current.
        /// </summary>
        /// <remarks></remarks>
        public RenderState CurrentState
        {
            get { return _renderStates.Peek(); }
        }

        /// <summary>
        /// Resets the states.
        /// </summary>
        /// <remarks></remarks>
        public void ResetStates()
        {
            _renderStates.Clear();
            ChangeState(new PlayingRenderState(Game, this));
        }
    }
}